package com.arm.controller.api.product;

import com.arm.controller.WechatBaseController;
import com.arm.def.Constant;
import com.arm.module.Product;
import com.arm.service.ProductService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Baggio on 2019-1-8.
 */
@Api(description = "产品接口")

@RestController
@RequestMapping("/api/product")
public class APIProductController extends WechatBaseController {
    @Autowired
    ProductService productService;

    @ApiOperation(value = "获取产品类型列表", notes = "获取产品类型列表")
    @GetMapping(value = "/productType/list")
    public String getProductTypeList() {
        ResultObj resultObj = new ResultObj();
        try {
            resultObj.setData(productService.getProductTypeList());
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj.toJson();


    }   @ApiOperation(value = "获取产品列表", notes = "获取产品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "int")
    })
    @GetMapping(value = "/list")
    public ResultObj getProductList(Integer page, Integer pageSize) {
        ResultObj resultObj = new ResultObj();

        try {

            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            PageHelper.startPage(page, pageSize);

            List<Product> productList = productService.getProductList(null);

            PageInfo<Product> pageinfo = new PageInfo<Product>(productList);
            resultObj.setData(pageinfo);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj;


    }

    @ApiOperation(value = "获取产品", notes = "获取产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品Id", required = true, paramType = "path", dataType = "Long")
    })
    @GetMapping(value = "/{productId}")
    public ResultObj getProductByProductId(@PathVariable("productId") Long productId) {
        ResultObj resultObj = new ResultObj();
        try {

            resultObj.setData(productService.getProduct(new HashMap<String, String>() {{
                put("productId", String.valueOf(productId));

            }}));

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj;
    }

    @ApiOperation(value = "添加、编辑产品", notes = "获取产品")
    @PostMapping(value = "/modifyProduct")
    public String modifyProduct(@RequestBody Product product) {
        ResultObj resultObj = new ResultObj();
        try {
            productService.modifyProduct(product);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
